package com.mentalhealthplatform.teacherV2.service;

import com.mentalhealthplatform.appModels.Article;
import com.mentalhealthplatform.global.elasticsearch.ArticleElasticsearchV2;
import com.mentalhealthplatform.teacherV2.repository.ArticleRepositoryV2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ArticleServiceV2 {

    @Autowired
    private ArticleRepositoryV2 articleRepository;

    @Autowired
    private ArticleElasticsearchV2 articleElasticsearch;

    // 获取所有文章的方法
    public List<Article> getAllArticles() {
        try {
            List<Article> articles = articleRepository.findAll();
            if (articles.isEmpty()) {
                System.out.println("No articles found");
            }
            return articles;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    // 根据ID获取文章的方法
    public Article getArticleById(Long id) {
        return articleRepository.findById(id).orElse(null);
    }

    // 根据查询内容搜索文章的方法
    public List<Article> searchArticles(String query) {
        return articleElasticsearch.findByTitleContainingOrContentContaining(query, query);
    }
}